package org.niu.stack;

import java.util.Stack;

public class IsValid {
    public boolean isValid (String s) {
        // write code here
        Stack<Character> stack = new Stack<>();
        int len = s.length();
        if((len&1) == 1) return false;
        if(s.charAt(0) == '}'||s.charAt(0) == ')' || s.charAt(0) == ']') return false;
        for (int i = 0; i < s.length();i++){
            if(s.charAt(i) == '['||s.charAt(i) == '{'||s.charAt(i)=='('){
                stack.push(s.charAt(i));
            }else if(s.charAt(i) == ']'){
                Character head = stack.pop();
                if(head!='['){
                    return false;
                }
            }else if(s.charAt(i) == '}'){
                Character head = stack.pop();
                if(head!='{'){
                    return false;
                }
            }else if(s.charAt(i) == ')'){
                Character head = stack.pop();
                if(head!='('){
                    return false;
                }
            }
        }
        return true;
    }
}
